package org.etom.impl.connector.util;

import org.etom.impl.connector.datatypes.MoodleCourseImpl;
import org.etom.impl.connector.datatypes.MoodleModuleImpl;
import org.etom.impl.connector.datatypes.MoodleUserImpl;

import redstone.xmlrpc.XmlRpcStruct;

/**
 * An utility class which is responsible for the parsing of the server xml responses.
 */
class ResponseParser {
  
  private static final String KEY_COURSE_ID       = "id";
  private static final String KEY_COURSE_FULLNAME = "fullname";
  
  private static final String KEY_USER_ID = "id";
  
  private static final String KEY_MODULE_ID        = "instance";
  private static final String KEY_MODULE_NAME      = "name";
  private static final String KEY_MODULE_REFERENCE = "reference";
  private static final String KEY_MODULE_DESC      = "description";
  
  private XmlRpcClientWrapper xmlRpcClientWrapper;
  
  /**
   * Construct a new parser
   * 
   * @param xmlRpcClientWrapper - the xml rpc wrapper
   */
  public ResponseParser(XmlRpcClientWrapper xmlRpcClientWrapper) {
    this.xmlRpcClientWrapper = xmlRpcClientWrapper;
  }
  
  /**
   * The method parses the Moodle course.
   * 
   * @param rpcCourse - the course response
   * 
   * @return - the parsed course response
   */
  public MoodleCourseImpl parseMoodleCourse(XmlRpcStruct rpcCourse) {
    MoodleCourseImpl moodleCourse = new MoodleCourseImpl(
        rpcCourse.getString(KEY_COURSE_FULLNAME),
        Long.valueOf(rpcCourse.getString(KEY_COURSE_ID)),
        xmlRpcClientWrapper);
    return moodleCourse;
  }
  
  /**
   * The method parses the Moodle user.
   * 
   * @param rpcStruct - the user response
   * 
   * @return - the parsed user response
   */
  public MoodleUserImpl parseMoodleUser(XmlRpcStruct rpcStruct) {
    MoodleUserImpl user = new MoodleUserImpl(
        Long.valueOf(rpcStruct.getString(KEY_USER_ID)),
        xmlRpcClientWrapper);
    return user;
  }
  
  /**
   * The method parses the Moodle course.
   * 
   * @param rpcStruct - the course response
   * 
   * @return the parsed user response
   */
  public MoodleModuleImpl parseMoodleCourseModule(XmlRpcStruct rpcStruct) {
    MoodleModuleImpl module = new MoodleModuleImpl(
        Long.valueOf(rpcStruct.get(KEY_MODULE_ID).toString()),
        rpcStruct.getString(KEY_MODULE_NAME),
        rpcStruct.getString(KEY_MODULE_REFERENCE),
        rpcStruct.getString(KEY_MODULE_DESC),
        xmlRpcClientWrapper);
    return module;
  }
  
}
